
# generate appendix C1, tables C1 and C2

library(rio) # to load data
library(tidyverse) # to reshape data
library(AER) # to run tobit
library(lmtest) # to cluster standard errors
library(sandwich) # to cluster standard errors
library(texreg) # to generate tables

# set your working directory
setwd("~/replication_files")

monthly_data <- import("data/monthly_data.csv")

# table C1

# model 1
probit1 <- glm(sovereign_issued_1yr ~
               resource_rents_perc_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit1_r <- probit1 %>%
  coeftest(vcovHC(probit1, type = 'HC0', cluster = c('iso3c','Summary')))

# model 2
probit2 <- glm(sovereign_issued_1yr ~
               resource_rents_perc_lag +
               minister_is_econ_technocrat_usa +
               minister_turnover_5yrs +
               debt_crisis_experience +
               election_month + left_exec +
               fiscal_council + 
               polcon + 
               imf_agreement +
               fiscal_balance_lag + 
               tax_perc_gdp_cepal_lag +
               log_core_inflation_lag +
               gdp_pc_cepal_lag +
               gdp_growth_cepal_lag +
               ka_open_lag +
               log_reserves_lag +
               treasury_rate_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit2_r <- probit2 %>%
  coeftest(vcovHC(probit2, type = 'HC0', cluster = c('iso3c','Summary')))

# model 3
probit3 <- glm(sovereign_issued_1yr ~
               log_oil_production_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit3_r <- probit3 %>%
  coeftest(vcovHC(probit3, type = 'HC0', cluster = c('iso3c','Summary')))

# model 4
probit4 <- glm(sovereign_issued_1yr ~
               log_oil_production_lag +
               minister_is_econ_technocrat_usa +
               minister_turnover_5yrs +
               debt_crisis_experience +
               election_month + left_exec +
               fiscal_council + 
               polcon + 
               imf_agreement +
               fiscal_balance_lag + 
               tax_perc_gdp_cepal_lag +
               log_core_inflation_lag +
               gdp_pc_cepal_lag +
               gdp_growth_cepal_lag +
               ka_open_lag +
               log_reserves_lag +
               treasury_rate_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit4_r <- probit4 %>%
  coeftest(vcovHC(probit4, type = 'HC0', cluster = c('iso3c','Summary')))

# model 5
probit5 <- glm(sovereign_issued_1yr ~
               terms_of_trade_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit5_r <- probit5 %>%
  coeftest(vcovHC(probit5, type = 'HC0', cluster = c('iso3c','Summary')))

# model 6
probit6 <- glm(sovereign_issued_1yr ~
               terms_of_trade_lag +
               minister_is_econ_technocrat_usa +
               minister_turnover_5yrs +
               debt_crisis_experience +
               election_month + left_exec +
               fiscal_council + 
               polcon + 
               imf_agreement +
               fiscal_balance_lag + 
               tax_perc_gdp_cepal_lag +
               log_core_inflation_lag +
               gdp_pc_cepal_lag +
               gdp_growth_cepal_lag +
               ka_open_lag +
               log_reserves_lag +
               treasury_rate_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit6_r <- probit6 %>%
  coeftest(vcovHC(probit6, type = 'HC0', cluster = c('iso3c','Summary')))

# model 7
probit7 <- glm(sovereign_issued_1yr ~
               discovery_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit7_r <- probit7 %>%
  coeftest(vcovHC(probit7, type = 'HC0', cluster = c('iso3c','Summary')))

# model 8
probit8 <- glm(sovereign_issued_1yr ~
               discovery_lag +
               minister_is_econ_technocrat_usa +
               minister_turnover_5yrs +
               debt_crisis_experience +
               election_month + left_exec +
               fiscal_council + 
               polcon + 
               imf_agreement +
               fiscal_balance_lag + 
               tax_perc_gdp_cepal_lag +
               log_core_inflation_lag +
               gdp_pc_cepal_lag +
               gdp_growth_cepal_lag +
               ka_open_lag +
               log_reserves_lag +
               treasury_rate_lag +
               I(iso3c) +
               time,
               data = monthly_data,
               family = binomial(link = "probit"))

probit8_r <- probit8 %>%
  coeftest(vcovHC(probit8, type = 'HC0', cluster = c('iso3c','Summary')))

# generate table C1
texreg(l = list(probit1_r,probit2_r,probit3_r,probit4_r,probit5_r,probit6_r,probit7_r,probit8_r), stars = c(0.01, 0.05, 0.1),
       custom.header = list("Dependent Variable:" = 1:8),
       custom.coef.map = list("resource_rents_perc_lag" = "Resource Rents, % of GDP $_{t-1}$",
                              "log_oil_production_lag" = "Ln Oil and Gas Production $_{t-1}$",
                              "terms_of_trade_lag" = "Commodity Price Index $_{t-1}$",
                              "discovery_lag" = "Field Discovery $_{t-1}$",
                              "minister_is_econ_technocrat_usa" = "Mainstream Minister = 1",
                              "minister_turnover_5yrs" = "Minister Turnover (5 Years)",
                              "debt_crisis_experience" = "Debt Crisis Experience = 1",
                              "election_month" = "Election Month = 1",
                              "left_exec" = "Left Executive = 1",
                              "cb_independence" = "Central Bank Independence",
                              "fiscal_council" = "Fiscal Council = 1",
                              "polcon" = "Political Constraints",
                              "imf_agreement" = "IMF Agreement = 1",
                              "fiscal_balance_lag" = "Fiscal Balance, % of GDP $_{t-1}$",
                              "tax_perc_gdp_cepal_lag" = "Tax Revenue, % of GDP $_{t-1}$",
                              "log_core_inflation_lag" = "Ln Core Inflation $_{t-1}$",
                              "gdp_pc_cepal_lag" = "GDP Per Capita $_{t-1}$",
                              "gdp_growth_cepal_lag" = "GDP Growth, % $_{t-1}$",
                              "ka_open_lag" = "Capital Openness $_{t-1}$",
                              "log_reserves_lag" = "Ln International Reserves $_{t-1}$",
                              "treasury_rate_lag" = "US Treasury Rate, % $_{t-1}$"),
       fontsize = "footnotesize", digits = 3)

# retrieve fit stats
texreg(l = list(probit1,probit2,probit3,probit4,probit5,probit6,probit7,probit8), stars = c(0.01, 0.05, 0.1))


# table C2

# model 1
tobit1 <- tobit(log_sovereign_issued_1yr_amount ~
                resource_rents_perc_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 2
tobit2 <- tobit(log_sovereign_issued_1yr_amount ~
                resource_rents_perc_lag +
                minister_is_econ_technocrat_usa +
                minister_turnover_5yrs +
                debt_crisis_experience +
                election_month + left_exec +
                fiscal_council + 
                polcon + 
                imf_agreement +
                fiscal_balance_lag + 
                tax_perc_gdp_cepal_lag +
                log_core_inflation_lag +
                gdp_pc_cepal_lag +
                gdp_growth_cepal_lag +
                ka_open_lag +
                log_reserves_lag +
                treasury_rate_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 3
tobit3 <- tobit(log_sovereign_issued_1yr_amount ~
                log_oil_production_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 4
tobit4 <- tobit(log_sovereign_issued_1yr_amount ~
                log_oil_production_lag +
                minister_is_econ_technocrat_usa +
                minister_turnover_5yrs +
                debt_crisis_experience +
                election_month + left_exec +
                fiscal_council + 
                polcon + 
                imf_agreement +
                fiscal_balance_lag + 
                tax_perc_gdp_cepal_lag +
                log_core_inflation_lag +
                gdp_pc_cepal_lag +
                gdp_growth_cepal_lag +
                ka_open_lag +
                log_reserves_lag +
                treasury_rate_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 5
tobit5 <- tobit(log_sovereign_issued_1yr_amount ~
                terms_of_trade_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 6
tobit6 <- tobit(log_sovereign_issued_1yr_amount ~
                terms_of_trade_lag +
                minister_is_econ_technocrat_usa +
                minister_turnover_5yrs +
                debt_crisis_experience +
                election_month + left_exec +
                fiscal_council + 
                polcon + 
                imf_agreement +
                fiscal_balance_lag + 
                tax_perc_gdp_cepal_lag +
                log_core_inflation_lag +
                gdp_pc_cepal_lag +
                gdp_growth_cepal_lag +
                ka_open_lag +
                log_reserves_lag +
                treasury_rate_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 7
tobit7 <- tobit(log_sovereign_issued_1yr_amount ~
                discovery_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# model 8
tobit8 <- tobit(log_sovereign_issued_1yr_amount ~
                discovery_lag +
                minister_is_econ_technocrat_usa +
                minister_turnover_5yrs +
                debt_crisis_experience +
                election_month + left_exec +
                fiscal_council + 
                polcon + 
                imf_agreement +
                fiscal_balance_lag + 
                tax_perc_gdp_cepal_lag +
                log_core_inflation_lag +
                gdp_pc_cepal_lag +
                gdp_growth_cepal_lag +
                ka_open_lag +
                log_reserves_lag +
                treasury_rate_lag +
                I(iso3c) +
                time,
                data = monthly_data, robust = T)

# generate table C2
texreg(l = list(tobit1,tobit2,tobit3,tobit4,tobit5,tobit6,tobit7,tobit8), stars = c(0.01, 0.05, 0.1),
       custom.coef.map = list("resource_rents_perc_lag" = "Resource Rents, % of GDP $_{t-1}$",
                              "log_oil_production_lag" = "Ln Oil and Gas Production $_{t-1}$",
                              "terms_of_trade_lag" = "Commodity Price Index $_{t-1}$",
                              "discovery_lag" = "Field Discovery $_{t-1}$",
                              "minister_is_econ_technocrat_usa" = "Mainstream Minister = 1",
                              "minister_turnover_5yrs" = "Minister Turnover (5 Years)",
                              "debt_crisis_experience" = "Debt Crisis Experience = 1",
                              "election_month" = "Election Month = 1",
                              "left_exec" = "Left Executive = 1",
                              "fiscal_council" = "Fiscal Council = 1",
                              "polcon" = "Political Constraints",
                              "imf_agreement" = "IMF Agreement = 1",
                              "fiscal_balance_lag" = "Fiscal Balance, % of GDP",
                              "tax_perc_gdp_cepal_lag" = "Tax Revenue, % of GDP $_{t-1}$",
                              "log_core_inflation_lag" = "Ln Core Inflation $_{t-1}$",
                              "gdp_pc_cepal_lag" = "GDP Per Capita $_{t-1}$",
                              "gdp_growth_cepal_lag" = "GDP Growth, % $_{t-1}$",
                              "ka_open_lag" = "Capital Openness $_{t-1}$",
                              "log_reserves_lag" = "Ln International Reserves $_{t-1}$",
                              "treasury_rate_lag" = "US Treasury Rate, % $_{t-1}$",
                              "Log(scale)" = "Log(Scale)"),
       fontsize = "footnotesize", digits = 3)

# model 1